/*import { getPermission } from '@/api/api'
import { login } from '@/service/api/common'
import { setItem, getItem, removeAllItem } from '@/utils/storage'
import { TOKEN, USERINFO,EMPPOSITION } from '@/constant'
import { setTimeStamp } from '@/utils/auth'
import { formatPermissionList, lowerCase } from '@/utils/index'
import router, { resetRouter } from '@/router'
import { ElMessage } from "element-plus";
import { useStore } from "vuex";


export default {
    namespaced: true,
    state: () => ({
        //token的初始值从storage里取
        token: getItem(TOKEN) || '',
        userInfo: getItem(USERINFO) || {},
        empPosition:getItem(EMPPOSITION) || {},
        roles: [],
        buttons: []
    }),
    mutations: {
        setToken(state, token) {
            state.token = token
            setItem(TOKEN, token)
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo
            setItem(USERINFO, userInfo)
        },
        setempPosition(state, empPosition) {
            state.empPosition = empPosition
            setItem(EMPPOSITION, empPosition)
        },
        setRoles: (state, roles) => {
            state.roles = roles
        },
        setButtons: (state, buttons) => {
            state.buttons = buttons
        },
    },
    actions: {
        async login(context, loginForm) {
            const data = await login(loginForm);
            const store = useStore();
            
            if (data) {
                //const redirectPath =context.state.roles.routes[0].path;
                this.commit('user/setToken', data.token);
                this.commit('user/setUserInfo', data.userInfo);
                this.commit('user/setempPosition',data.empPosition)
                router.push('/home');
                //router.push(redirectPath);
            }
            // return new Promise((resolve, reject) => {
            //     login({
            //         username,
            //         password,
            //         captcha_code,
            //         code_key,
            //     })
            //         .then(data => {
            //             this.commit('user/setToken', data.obj.sys_token)
            //             this.commit('user/setUserInfo', data.obj)
            //             // 保存登录时间
            //             setTimeStamp()
            //             resolve()
            //         })
            //         .catch(err => {
            //             reject(err)
            //         })
            //本地模拟数据
            // console.log("----模拟【登录】接口数据,真实数据需要填写constant.js里的接口域名------")
            // const loginData =import.meta.glob('@/api/loginData.json', { eager: true })
            // let obj = loginData['/src/api/loginData.json'].default
            // this.commit('user/setToken', obj.sys_token)
            // this.commit('user/setUserInfo', obj)
            // // 保存登录时间
            // setTimeStamp()
            // resolve()

            // })
        },
        getPermissionData(context) {
            return new Promise((resolve, reject) => {
                const roleId = context.state.userInfo.roleId;
                getPermission({roleId})
                    .then(data => {
                        console.log(data);
                        let obj = formatPermissionList(data.data)
                        let role_arr = obj.role_arr//菜单权限
                        let button_arr = obj.button_arr//button权限
                        let info = {
                            roles: role_arr
                        }
                        if (role_arr.length == 0) {
                            ElMessage.error("您登录的账号暂无权限！") // 提示错误信息
                            this.dispatch('user/logout')
                        }
                        this.commit('user/setRoles', role_arr)
                        this.commit('user/setButtons', button_arr)

                        console.log(context.role_arr);
                        resolve(info)
                    })
                    .catch(err => {

                    })
            })
        },
        logout() {
            resetRouter()
            this.commit('user/setToken', '')
            this.commit('user/setUserInfo', {})
            this.commit('user/setRoles', [])
            this.commit('user/setButtons', [])
            this.commit('app/removeTagsView', {
                type: 'all'
            })
            removeAllItem()
            router.push('/login')
        }
    }
}*/
import { getPermission } from '@/api/api'
import { login, getButton } from '@/service/api/common'
import { setItem, getItem, removeAllItem } from '@/utils/storage'
import { TOKEN, USERINFO, EMPPOSITION } from '@/constant'
import { setTimeStamp } from '@/utils/auth'
import { formatPermissionList, lowerCase } from '@/utils/index'
import router, { resetRouter } from '@/router'
import { ElMessage } from "element-plus";
import { useStore } from "vuex";


export default {
    namespaced: true,
    state: () => ({
        //token的初始值从storage里取
        token: getItem(TOKEN) || '',
        userInfo: getItem(USERINFO) || {},
        empPosition: getItem(EMPPOSITION) || {},
        roles: [],
        buttons: []
    }),
    mutations: {
        setToken(state, token) {
            state.token = token
            setItem(TOKEN, token)
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo
            setItem(USERINFO, userInfo)
        },
        setempPosition(state, empPosition) {
            state.empPosition = empPosition
            setItem(EMPPOSITION, empPosition)
        },
        setRoles: (state, roles) => {
            state.roles = roles
        },
        setButtons: (state, buttons) => {
            state.buttons = buttons
        },
    },
    actions: {
        async login(context, loginForm) {
            //console.log(loginForm);
            const data = await login(loginForm);
            // const store = useStore();

            if (data) {
                //const redirectPath =context.state.roles.routes[0].path;
                this.commit('user/setToken', data.token);
                this.commit('user/setUserInfo', data.userInfo);
                //this.commit('user/setempPosition', data.empPosition)
                router.push('/home');
                //router.push(redirectPath);
            }
            // return new Promise((resolve, reject) => {
            //     login({
            //         username,
            //         password,
            //         captcha_code,
            //         code_key,
            //     })
            //         .then(data => {
            //             this.commit('user/setToken', data.obj.sys_token)
            //             this.commit('user/setUserInfo', data.obj)
            //             // 保存登录时间
            //             setTimeStamp()
            //             resolve()
            //         })
            //         .catch(err => {
            //             reject(err)
            //         })
            //本地模拟数据
            // console.log("----模拟【登录】接口数据,真实数据需要填写constant.js里的接口域名------")
            // const loginData =import.meta.glob('@/api/loginData.json', { eager: true })
            // let obj = loginData['/src/api/loginData.json'].default
            // this.commit('user/setToken', obj.sys_token)
            // this.commit('user/setUserInfo', obj)
            // // 保存登录时间
            // setTimeStamp()
            // resolve()

            // })
        },
        getPermissionData(context) {
            return new Promise((resolve, reject) => {
                const roleId = context.state.userInfo.roleId;
                getPermission({ roleId })
                    .then(async data => {
                        //console.log(data);
                        let obj = formatPermissionList(data.data)
                        let role_arr = obj.role_arr//菜单权限
                        let id = context.state.userInfo.id
                        const { buttonCode } = await getButton(roleId);
                        let button_arr = buttonCode//button权限
                        let info = {
                            roles: role_arr,
                            button: button_arr
                        }
                        if (role_arr.length == 0) {
                            ElMessage.error("您登录的账号暂无权限！") // 提示错误信息
                            this.dispatch('user/logout')
                        }
                        this.commit('user/setRoles', role_arr)
                        this.commit('user/setButtons', button_arr)

                        //console.log(context.role_arr);
                        resolve(info)
                    })
                    .catch(err => {

                    })
            })
        },
        logout(context) {
            if (Object.keys(context.state.userInfo).length > 0) {
                resetRouter()
                this.commit('user/setToken', '')
                this.commit('user/setUserInfo', {})
                this.commit('user/setRoles', [])
                this.commit('user/setButtons', [])
                this.commit('app/removeTagsView', {
                    type: 'all'
                })
                removeAllItem()
                router.push('/login')
            }
        },
		
    }
}

